var Handler = laya.utils.Handler;
var Loader = laya.net.Loader;
Event = Laya.Event;
var userInfo = UserInfo.getInstance();

// 程序入口
var GameMain = (function () {
    function GameMain() {
        Laya.init(866, 1540, Laya.WebGL);
        laya.utils.Stat.hide();
        //设置适配模式
        Laya.stage.scaleMode = "fixedhight";
        Laya.stage.alignH = "middle";
        Laya.stage.alignV = "center";
        Laya.stage.screenMode = "vertical";
        //设置横竖屏
        if (laya.utils.Browser.onPC) {
            //Laya.stage.scaleMode = "showall";
            //Laya.stage.screenMode = "none";
        }
        else {
            Laya.stage.scaleMode = "fixedhight";
            Laya.stage.screenMode = "vertical";
        }

        setInterval(function () {
            var rw = laya.utils.Browser.width;
            var rh = laya.utils.Browser.height;

            if (rw == currWidth && rh == currHeight) {
                return;
            }
            if (laya.utils.Browser.onPC) {
                // return;
            }
            if (this.checkIsOnInput()) {
                return;
            }

            this.elementAdapt();

        }.bind(this), 10)

        Laya.loader.load([
            { url: "res/Package.fui?v=" + appConfig.updateVersion, type: Loader.BUFFER },
            { url: "res/Package_atlas0.png?v=" + appConfig.updateVersion, type: Loader.IMAGE },
            //{ url: "res/bg.png", type: Loader.IMAGE }
        ], Handler.create(this, this.onLoaded));

        protobuf.load("js/Message.proto?v=" + appConfig.updateVersion, onAssetsLoaded);

        function onAssetsLoaded(err, root) {
            var n = 0;
            n++;
            window.proot = root;
            var gameMsg = proot.lookup("pb.Msg");
            window.proto = root;

        };

    }

    var currWidth = 866;
    var currHeight = 1540;

    GameMain.prototype.checkIsOnInput = function () {
        for (var i in window.inputList) {
            var temp = window.inputList[i]
            if (temp.input._focus) {
                return true;
            }
        }
        return false;
    }

    GameMain.prototype.elementAdapt = function () {

        if (laya.utils.Browser.width > laya.utils.Browser.height) {
            rw = laya.utils.Browser.height;
            rh = laya.utils.Browser.width;
        }
        else {
            rw = laya.utils.Browser.width;
            rh = laya.utils.Browser.height;
        }



        var f = rw / rh;
        var f_Defalt = 866 / 1540;

        if (f >= f_Defalt) {
            Laya.stage.scaleMode = "fixedheight";
            var fs = rh / 1540;
            var w1 = rw / fs;
            if (window._view) {
                window._view.setSize(w1, 1540);
                window._view.scaleX = fs;
                window._view.scaleY = fs;

                currWidth = laya.utils.Browser.width;
                currHeight = laya.utils.Browser.height;
            }
        }
        else {
            Laya.stage.scaleMode = "fixedwidth";
            var fs = rw / 866;
            var h1 = rh / fs;
            if (window._view) {
                window._view.setSize(866, h1);
                window._view.scaleX = fs;
                window._view.scaleY = fs;

                currWidth = laya.utils.Browser.width;
                currHeight = laya.utils.Browser.height;
            }
        }

        Laya.init(rw, rh, Laya.WebGL);
        Laya.stage.width = rw;
        Laya.stage.height = rh;
        Laya.stage.setScreenSize(rw, rh);
    };

    GameMain.prototype.onLoaded = function () {
        Laya.stage.addChild(fairygui.GRoot.inst.displayObject);
        var list = ["res/Package.fui", "res/Package_atlas0.png"];
        Util.cacheResource(list);
        fairygui.UIPackage.addPackage("res/Package");
        fairygui.UIConfig.defaultFont = "微软雅黑";

        userInfo.removeToken();

        if (!userInfo.getToken()) {
            this.loginPanel = new UIPanel_login();
        }

        laya.tcpio = new wstcp('go');


        LoginMethod.getInstance().checkUrl(this.tryLoginReturn.bind(this));

    };
    GameMain.prototype.tryLoginReturn = function (type) {
        if (type === 'showDlgPhoneLogin') {
            return;
        }
        if (type === 'loginSuccess') {
            NetTcp.instance().loginGame();
            //this.gamePanel = new UIPanel_Main();
            return;
        }
        if (type === 'loginFailed') {
            console.log('登录失败, 需要重新登录');
            this.loginPanel = new UIPanel_login();
            return;
        }
    }


    return GameMain;
}());

var GM = new GameMain();
